package 哈希.赎金信383;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public static boolean canConstruct(String ransomNote, String magazine) {
        if(magazine.length() < 1 || ransomNote.length() < 1){
            return false;
        }
        Map<Character,Integer> map = new HashMap<>();
        for(int i=0;i<magazine.length();i++){
            map.put(magazine.charAt(i),map.getOrDefault(magazine.charAt(i),0)+1);
        }

        for(int i=0;i<ransomNote.length();i++){
            if(!map.containsKey(ransomNote.charAt(i))){
                return false;
            }

            if(map.get(ransomNote.charAt(i)) - 1 < 0){
                return false;
            }

            map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i))-1);

        }

        return true;

    }
    public static void main(String[] args) {
        String ransomNote = "aa";
        String magazine = "aab";
        System.out.println(canConstruct(ransomNote,magazine));
    }
}
